<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        .box {
            display: flex;
            justify-content: space-around;
            align-items: center;
            width: 400px;
            height: 150px;
            margin: 100px auto;
            background-color: #ccc;
            border-radius: 40px;
        }

        .box div {
            width: 80px;
            height: 80px;
            background-color: #eee;
            /* background-color: green; */
            border-radius: 50%;
        }

        #red.active {
            background-color: #f00;
        }

        #yellow.active {
            background-color: yellow;
        }

        #green.active {
            background-color: green;
        }

        .box .num {
            background-color: #ccc;
            border-radius: 0;
            font-size: 60px;
            line-height: 80px;
            text-align: center;
            color: #fff;
        }
    </style>
</head>

<body>
    <div class="box">
        <div id="red"></div>
        <div id="yellow"></div>
        <div id="green"></div>
        <div class="num">30</div>
    </div>

    <script>
        // 红绿灯倒计时

        // - 横向三色交通信号灯的亮灯的顺序一般为“绿->黄->红->绿”依次循环。
        // - 亮灯时长需根据路口的实际情况等因素来考虑设置，如红灯亮设置为30秒，绿灯亮设置为35秒，黄灯亮设置为5秒。
        // - 编写HTML，设置红黄绿灯及倒计时的显示页面。
        // - 创建红、黄、绿灯对象lamp，保存相关的数据。
        // - 创建倒计时的元素对象count，实现倒计时的时间设置。
        // - 根据lamp和count对象获取并设置绿灯亮时页面初始化效果。
        // - 利用setInterval()函数完成信号灯倒计时的动态改变效果。


        function Lamp(ele, time, nextlamp) {
            this.ele = ele;
            this.time = time;
            this.nextlamp = nextlamp;
        }

        Lamp.prototype.clearColor = function () {
            this.ele.className = '';
        }
        Lamp.prototype.changeColor = function () {
            this.ele.className = 'active';
        }

        Lamp.prototype.changeTime = function (time) {
            querySelector('.num').innerHTML = time < 10 ? '0' + time : time;
        }

        // 获取对象元素
        function querySelector(ele) {
            return document.querySelector(ele);
        }

        var green = new Lamp(querySelector('#green'), 3, 'yellow');
        var yellow = new Lamp(querySelector('#yellow'), 2, 'red');
        var red = new Lamp(querySelector('#red'), 5, 'green');

        var nowLamp = green;
        var time = nowLamp.time;
        nowLamp.changeColor();
        nowLamp.changeTime(time);
        setInterval(function () {
            time--;
            if (time <= 0) {
                nowLamp.clearColor();
                nowLamp = window[nowLamp.nextlamp];
                time = nowLamp.time;
                nowLamp.changeColor();
            }
            nowLamp.changeTime(time);
        }, 1000);


    </script>


</body>

</html>